// pages/send/send.js
const api = require("../../api/api")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgs: [],
    fileID: [],
    goodsName: '',
    price: '',
    goodsMsg: ''


  },
  /*----------------------------------------------------------------------------- */
  chooseImg() {
    let that = this;
    let len = this.data.imgs;
    if (len >= 3) {
      this.setData({
        lenMore: 1
      })
      return;
    }
    wx.chooseImage({
      sourceType: ['album', 'camera'],
      count: 3 - this.data.imgs.length,
      success: (res) => {
        let tempFilePaths = res.tempFilePaths;
        let imgs = that.data.imgs;
        for (let i = 0; i < tempFilePaths.length; i++) {
          if (imgs.length < 9) {
            imgs.push(tempFilePaths[i])
          } else {
            that.setData({
              imgs
            })
            return;
          }
        }
        that.setData({
          imgs
        })
        console.log(this.data.imgs)
      }
    })
  },
  previewImg(e) {
    let index = e.currentTarget.dataset.index;
    let imgs = this.data.imgs;
    wx.previewImage({
      current: imgs[index],
      urls: imgs,
    })
  },
  deleteImg(e) {
    let _index = e.currentTarget.dataset.index;
    let imgs = this.data.imgs;
    imgs.splice(_index, 1);
    this.setData({
      imgs
    })
  },
  sendIt(e) {
    
  },
  /*----------------------------------------------------------------------------- */

  subminGoods(e) {
    if (!app.globalData.isLogin) {
      wx.showToast({
        title: '请登录后操作',
        icon: "none"
      })
      return
    }
    var info = e.detail.value
    if (!info.goodsName) {
      wx.showToast({
        title: '请输入商品名称',
        icon: 'none'
      })
      return
    }
    if (!info.price) {
      wx.showToast({
        title: '请输入商品价格',
        icon: 'none'
      })
      return
    }
    if (!info.goodsMsg) {
      wx.showToast({
        title: '请输入商品信息',
        icon: 'none'
      })
      return
    }
    if (!this.data.imgs.length) {
      wx.showToast({
        title: '请上传图片',
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '发布中...',
    })
    let fileID = this.data.fileID
    var str = ''
    Promise.all(this.data.imgs.map((item) => {
        return wx.cloud.uploadFile({
          cloudPath: 'goodsImage/' + Date.now() + item.match(/\.[^.]+?$/)[0], // 文件名称 
          filePath: item,
        })
      }))
      .then((resCloud) => {
        for (let index = 0; index < resCloud.length; index++) {
          fileID.push(resCloud[index].fileID)
        }
        Promise.all(fileID.map((item) => {
            return wx.cloud.getTempFileURL({
              fileList: [{
                maxAge: 3600 * 360,
                fileID: item
              }]
            })
          }))
          .then((res) => {

            for (let index = 0; index < res.length; index++) {
              const element = res[index].fileList[0].tempFileURL;
              if (!str) {
                str = element
              } else {
                str = str + '|' + element
              }
            }
           

            wx.request({
              url: api.addGoods.url,
              method: api.addGoods.method,
              data: {
                name: info.goodsName,
                pro: info.goodsMsg,
                price: info.price,
                user_id: app.globalData.userInfo.id,
                img: str
              },
              success: res => {
                wx.hideLoading()
                wx.showToast({
                  title: '发布成功',
                  icon: 'success'
                })
                this.setData({
                  imgs: [],
                  fileID: [],
                  goodsName: '',
                  price: '',
                  goodsMsg: ''
                })
              }
            })
          })
      })

    console.log(e)
  },
  
})